package com.zyzd.pathdemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by 李宗源 on 2017/5/3.
 */

public class TestView extends View {
    public TestView(Context context) {
        this(context, null);
    }

    public TestView(Context context, AttributeSet attrs) {
        this(context, null,0);
    }

    public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {


    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawColor(Color.WHITE);

        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);

        paint.setTextSize(50);
        String testText = "你好";
        String str = testText + "（默认）";
        canvas.drawText(str, 0, str.length(), 0, 100, paint);

        paint.setAntiAlias(false);
        paint.setDither(false);
        paint.setSubpixelText(false);

        str = testText + "（去除所有效果）";
        canvas.drawText(str, 0, str.length(), 0, 200, paint);

        paint.setAntiAlias(true);
        str = testText + "（添加抗锯齿）";
        canvas.drawText(str, 0, str.length(), 0, 300, paint);

        paint.setDither(true);
        str = testText + "（添加抖动）";
        canvas.drawText(str, 0, str.length(), 0, 400, paint);

        paint.setSubpixelText(true);
        str = testText + "（添加亚像素）";
        canvas.drawText(str, 0, str.length(), 0, 500, paint);

        paint.setUnderlineText(true);
        str = testText + "（添加下划线）";
        canvas.drawText(str, 0, str.length(), 0, 600, paint);

        paint.setStrikeThruText(true);
        str = testText + "（添加删除线）";
        canvas.drawText(str, 0, str.length(), 0, 700, paint);

        paint.setFakeBoldText(true);
        str = testText + "（添加文本粗体）";
        canvas.drawText(str, 0, str.length(), 0, 800, paint);

        paint.setTextAlign(Paint.Align.LEFT);
        str = testText + "（设置中间）";
        canvas.drawText(str, 0, str.length(), 0, 900, paint);

        paint.setTextAlign(Paint.Align.CENTER);
        str = testText + "（设置中间）";
        canvas.drawText(str, 0, str.length(), 0, 1000, paint);

        paint.setTextAlign(Paint.Align.RIGHT);
        str = testText + "（设置中间）";
        canvas.drawText(str, 0, str.length(), 0, 1100, paint);

        int width = 600;
        int height = 50;
        paintR(canvas, paint, width, height);

        height += 50;
        paint.setStyle(Paint.Style.STROKE);
        paintR(canvas, paint, width, height);

        paint.setStyle(Paint.Style.FILL);

        height += 50;
        paint.setStrokeCap(Paint.Cap.ROUND);
        paintR(canvas, paint, width, height);

        height += 50;
        paint.setStrokeCap(Paint.Cap.SQUARE);
        paintR(canvas, paint, width, height);


        paint.setStyle(Paint.Style.STROKE);

        height += 150;
        paint.setStrokeJoin(Paint.Join.BEVEL);
        paintJOIN(canvas, paint, width, height);

        height += 150;
        paint.setStrokeJoin(Paint.Join.MITER);
        paintJOIN(canvas, paint, width, height);

        height += 150;
        paint.setStrokeJoin(Paint.Join.ROUND);
        paintJOIN(canvas, paint, width, height);

    }


    private void paintR(Canvas canvas, Paint paint, int width, int height) {
        paint.setStrokeWidth(20);
        canvas.drawLine(width, height, width + 200, height, paint);
    }
    private void paintJOIN(Canvas canvas, Paint paint, int width, int height) {
            /*画一个实心梯形*/
        Path path = new Path();
        path.moveTo(width, height);
        path.lineTo(width + 50, height);
        path.lineTo(width + 100, height + 50);
        path.lineTo(width, height + 50);
        path.close();
        canvas.drawPath(path, paint);
    }
}
